package gis;

import cn.hutool.core.util.CoordinateUtil;

public class GisDemo {




    public static void main(String[] args) {

        // 新奥广场
        CoordinateUtil.Coordinate coordinate3 = CoordinateUtil.gcj02ToWgs84(120.051847, 32.183823);
        System.out.println(coordinate3); // Coordinate{lng=120.04688156939316, lat=32.185725726162374}

        // POINT (13358992.8895998 3785860.3228001595) 泰兴大桥

        // 封庄桥
        // MULTILINESTRING ((13390661.423699856 3785741.969500065, 13390681.950099945 3785735.8873000145))
        // 13361209.27549982 3785467.305900097

        double mercatorX = 13358992.8895998;
        double mercatorY = 3785860.3228001595;

//        double mercatorX = 13361209.27549982;
//        double mercatorY = 3785467.305900097;
        CoordinateUtil.Coordinate coordinate00 = CoordinateUtil.mercatorToWgs84(mercatorX, mercatorY);
//        System.out.println(coordinate00);

        CoordinateUtil.Coordinate coordinate11 = CoordinateUtil.gcj02ToWgs84(coordinate00.getLng(), coordinate00.getLat());
        System.out.println(coordinate11);

//        System.out.println("墨卡托转化");
//        MULTILINESTRING ((13390661.423699856 3785741.969500065, 13390681.950099945 3785735.8873000145))
//        CoordinateUtil.Coordinate coordinate2 = CoordinateUtil.mercatorToWgs84(13390661.423699856d, 3785741.969500065d);

        // 13360991.236591 3784868.279214
//        CoordinateUtil.Coordinate coordinate2 = CoordinateUtil.mercatorToWgs84(13360992.547328d, 3784870.161124d);
//        System.out.println(coordinate2);
//
//        CoordinateUtil.Coordinate coordinate = CoordinateUtil.gcj02ToWgs84(coordinate2.getLng(), coordinate2.getLat());
//        System.out.println(coordinate);
//
//        double lng1 = coordinate11.getLng();
//        double lat1 = coordinate11.getLat();

        double lng1 = 120.009144;
        double lat1 = 32.171768;
        CoordinateUtil.Coordinate coordinate1 = GeoTransformTools.cGCS2000transTo(CoordinateEnum.GCJ023857,
                lng1, lat1);
        System.out.println(coordinate1);
        double lng = coordinate1.getLng();
        double lat = coordinate1.getLat();



        // 坐标1 (x1, y1)
        double x1 = mercatorX;
        double y1 = mercatorY;

        // 坐标2 (x2, y2)
        double x2 = lng;
        double y2 = lat;

        // 计算距离
        double dx = x2 - x1; // X方向差值
        double dy = y2 - y1; // Y方向差值
        double distance = Math.sqrt(dx * dx + dy * dy); // 欧几里得距离

        // 两点之间的距离: 2.2933859249062074 米
        System.out.println("两点之间的距离: " + distance + " 米");


        /*

            "sg_position": "河北省廊坊市广阳区南尖塔镇孔雀城枫景园",
            "latitude": "4802372.670520317",
            "locCode": "A00199",
            "locName": "中心网格",
            "longitude": "1.298846047842424E7",
         */
        // GCJ02 转成 墨卡托

        // 墨卡托 转成 GCJ02
        CoordinateUtil.Coordinate x = transToCGCS2000(CoordinateEnum.GCJ023857, 1.298846047842424E7, 4802372.670520317);
        System.out.println(x);

        System.out.println(cGCS2000transTo(CoordinateEnum.GCJ023857,x.getLng(),x.getLat()));

        // GCJ02 转成 CGCS2000
        System.out.println();

/*
        "location": "江苏省泰州市泰兴市姚王街道345国道新能源文江印",
        "latitude": "3783685.317541736",
        "longitude": "13364960.810325112",
 */
        // 经纬度 转墨卡托
        CoordinateUtil.Coordinate x3 = cGCS2000transTo(CoordinateEnum.GCJ023857, 120.054519, 32.156979);
        // Coordinate{lng=1.3364960810325112E7, lat=3783685.317541736}
        System.out.println(x3);

        double lng2 = x3.getLng();
        double lat2 = x3.getLat();

        // 墨卡托 转 20000
        CoordinateUtil.Coordinate coordinate = transToCGCS2000(CoordinateEnum.GCJ023857, lng2, lat2);
        // Coordinate{lng=120.0545131508584, lat=32.15696972158263}
        System.out.println(coordinate);
        // 结论 通过墨卡托转化,经纬度 在4位小数之后发生变形


        /*
          "address": "河北省廊坊市广阳区南尖塔镇孔雀城枫景园",
          "addressGeom": "1.298846047842424E7,4802372.670520317",
         */
        //
//        CoordinateUtil.Coordinate mercatorTogcj02 = CoordinateUtil.mercatorToWgs84(lon, lat);
        CoordinateUtil.Coordinate mercatorTogcj02 = CoordinateUtil.mercatorToWgs84(1.298846047842424E7, 4802372.670520317);
        System.out.println(mercatorTogcj02);
        CoordinateUtil.Coordinate coordinate2 = CoordinateUtil.wgs84ToBd09(mercatorTogcj02.getLng(), mercatorTogcj02.getLat());
        System.out.println(coordinate2);


    }



    public static CoordinateUtil.Coordinate cGCS2000transTo(CoordinateEnum targetCoordinate, double lon, double lat) {
        switch (targetCoordinate) {
            case CGCS2000:
                return new CoordinateUtil.Coordinate(lon, lat);
            case GCJ02:
                return CoordinateUtil.wgs84ToGcj02(lon, lat);
            case EPSG3857:
                return CoordinateUtil.wgs84ToMercator(lon, lat);
            case BD09:
                return CoordinateUtil.wgs84ToBd09(lon, lat);
            case GCJ023857:
                CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToGcj02(lon, lat);
                return CoordinateUtil.wgs84ToMercator(coordinate.getLng(), coordinate.getLat());
            default:
                throw new RuntimeException("暂不支持此种经纬度类型的转换!");
        }
    }

    public static CoordinateUtil.Coordinate transToCGCS2000(CoordinateEnum originCoordinate, double lon, double lat) {
        switch (originCoordinate) {
            case CGCS2000:
                return new CoordinateUtil.Coordinate(lon, lat);
            case GCJ02:
                return CoordinateUtil.gcj02ToWgs84(lon, lat);
            case EPSG3857:
                return CoordinateUtil.mercatorToWgs84(lon, lat);
            case BD09:
                return CoordinateUtil.bd09toWgs84(lon, lat);
            case GCJ023857:
                CoordinateUtil.Coordinate mercatorTogcj02 = CoordinateUtil.mercatorToWgs84(lon, lat);
                return CoordinateUtil.gcj02ToWgs84(mercatorTogcj02.getLng(), mercatorTogcj02.getLat());
            default:
                throw new RuntimeException("暂不支持此种经纬度类型的转换!");
        }
    }

}
